package com.cocoiko.tools.shoputils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cocoiko.info.ShopInfo;
import com.cocoiko.utils.CocoikoUtils;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;

import android.graphics.Bitmap;
import android.util.Log;

public class ShopInfoRequest {

	public ShopInfoRequest() {
		// TODO Auto-generated constructor stub
	}

	public static String httpGetRequestExe(String url) {

		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet();

		try {
			request.setURI(new URI(url));
			HttpResponse response = client.execute(request);

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = reader.readLine()) != null) {
				sb.append(line + NL);
			}
			reader.close();
			String page = sb.toString();
			Log.i("Request", url);
			Log.i("Response", page);

			return page;
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	public static ArrayList<ShopInfo> getNearShop(double lat, double log,
			int page) {

		ArrayList<ShopInfo> infos;
		String URL = "http://alike.jp/api/tenant/gettenantlist?key=alikeapp";

		String request_url = URL + "&category=2&limit=10" + "&lat=" + lat
				+ "&lng=" + log + "&format=json" + "&page=" + page;

		try {
			String data = httpGetRequestExe(request_url);

			if (data != null) {
				JSONObject json = Util.parseJson(data);
				JSONObject results = json.getJSONObject("result");
				// JSONObject general = results.getJSONObject("general");
				JSONArray tenants = results.getJSONArray("tenants");
				infos = jsonArrayToShopInfos(tenants);
				return infos;
			}
			return null;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (FacebookError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	public static ArrayList<ShopInfo> searchShop(String keyword, int page) {

		ArrayList<ShopInfo> infos;
		String URL = "http://alike.jp/api/tenant/gettenantlist?key=alikeapp";

		String request_url = URL + "&category=2&limit=10" + "&keyword="
				+ keyword + "&format=json" + "&page=" + page;

		try {
			String data = httpGetRequestExe(request_url);

			if (data != null) {
				JSONObject json = Util.parseJson(data);
				JSONObject results = json.getJSONObject("result");
				// JSONObject general = results.getJSONObject("general");
				JSONArray tenants = results.getJSONArray("tenants");

				infos = jsonArrayToShopInfos(tenants);
				return infos;
			}
			return null;

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (FacebookError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * @param tenants
	 * @return
	 * @throws JSONException
	 * @throws MalformedURLException
	 */
	public static ArrayList<ShopInfo> jsonArrayToShopInfos(JSONArray tenants)
			throws JSONException, MalformedURLException {
		ArrayList<ShopInfo> infos = new ArrayList<ShopInfo>();
		for (int i = 0; i < tenants.length(); i++) {
			JSONObject inf = tenants.getJSONObject(i);
			ShopInfo shop = new ShopInfo();
			shop.tid = inf.getInt("id");
			shop.name = inf.getString("name");
			shop.address = inf.getString("area_1");
			String s = inf.getString("photo_url");
			if (s != null && !s.equals("")) {
				java.net.URL url = new java.net.URL(s);
				Bitmap bitmap = CocoikoUtils.getBitmapFromUrl(url);
				shop.icon = bitmap;
			}
			infos.add(shop);
		}

		return infos;
	}

}
